<?php
	require_once("user_session.php");
	
	$template = new Template();
	$template->set_file("user","template/user.html");
	
	//set page title
	$template->set_var("pageTitle",SITENAME);
	
	$template->set_var("note", "");
	
	if(!isset($_SESSION["position"])){
		$_SESSION["position"] = "document.php?id=_0&method=v";
	}
	$template->set_var("backLink", $_SESSION["position"]);

    // method:
	// c: create new user (register)
	// e: edit user (modify user information)
	// v: view user (user view) 
	$type = $_GET["method"];
	$id = $_GET["userid"];
	
	if($type!="c" && $type!="e" && $type!="v")
	{
		$note = "<script language=javascript>\n".
				"alert(\"Missing parameters! Please visit this page from hyperlinks!\");\n".
				"history.go(-1);\n".
				"</script>";
		$template->set_var("note", $note);
	}
	 
	if($type=="c")
	{
		if(isset($_POST["username"]))
		{
			// this is the case of submitting form
			try{
				EKUser::register($_POST["userid"],
			                 $_POST["password"],
			                 $_POST["username"],
			                 $_POST["email"],
			                 $_POST["selfIntro"]);
			                 
			    $note = "<script language=javascript>\n".
				"alert(\"".$_POST["username"].", your account information will be checked by administrator later.\");\n".
				"history.go(-2);\n".
				"</script>";
				$template->set_var("note", $note);
				$_SESSION = array();
	    		
			}catch(Exception $e)
			{
				$note = "<script language=javascript>\n".
				        "alert(\"".$e->getMessage()."\");\n".
				        "history.go(-1);\n".
				        "</script>";
				$template->set_var("note", $note);
			}

		}else{
			// this is the case of filling form
			$template->set_var("title", "Registration");
			$template->set_var("params", "?method=c");
			$template->set_var("userId", "");
			$template->set_var("name", "");
			$template->set_var("email", "");
			$template->set_var("selfIntro", "");
			
			$template->set_var("exp", "");
			$template->set_var("ratingA", "");
			$template->set_var("ratingB", "");
			
			$template->set_var("idView", "");
			$template->set_var("idViewSpan", "none");
			
			$template->set_var("formView", "");
			$template->set_var("formViewSpan", "none");
			
			$template->set_var("expView", "none");
			$template->set_var("pwdView", "");
			$template->set_var("buttonView", "");
			$template->set_var("pageType", "c");
		}
	}
	
	if($type=="e")
	{
		if(!isset($_SESSION["userid"]) || $_SESSION["userid"]=="")
		{
			$template->set_var("contentView", "none");
		}
		if(isset($_POST["username"]))
		{
			// this is the case of submitting form
			try{
				EKUser::updateDB($_SESSION["userid"],"password",$_POST["password"]);
				EKUser::updateDB($_SESSION["userid"],"name",$_POST["username"]);
				EKUser::updateDB($_SESSION["userid"],"email",$_POST["email"]);
				EKUser::updateDB($_SESSION["userid"],"selfIntro",$_POST["selfIntro"]);
				$note = "<script language=javascript>\n".
				"alert(\"The personal information has been modified successfully!\");\n".
				"self.location=\"user.php?userid=".$_POST["userid"]."&method=v\"\n".
				"</script>";
				$template->set_var("note", $note);
			}catch(Exception $e)
			{
				$note = "<script language=javascript>\n".
				        "alert(\"".$e->getMessage()."\");\n".
				        "history.go(-1);\n".
				        "</script>";
				$template->set_var("note", $note);
			}
		}else{
			// this is the case of editing form	
			$id = $_SESSION["userid"];
			setTemplateWithSession($template);
			try{
				//construct new user instance
				$currentUser = new EKUser($id);
				
				$template->set_var("title", "Personal information modification");
				$template->set_var("params", "?method=e");
				$template->set_var("userId", $id);
				$template->set_var("name", $currentUser->getName());
				$template->set_var("email", $currentUser->getEmail());
				$template->set_var("selfIntro", $currentUser->getSelfIntro());
			}catch(Exception $e)
			{
				$note = "<script language=javascript>\n".
				        "alert(\"".$e->getMessage()."\");\n".
				        "history.go(-1);\n".
				        "</script>";
				$template->set_var("note", $note);
			}
				
			$template->set_var("exp", "");
			$template->set_var("ratingA", "");
			$template->set_var("ratingB", "");
			
			$template->set_var("idView", "none");
			$template->set_var("idViewSpan", "");
			
			$template->set_var("formView", "");
			$template->set_var("formViewSpan", "none");
				
			$template->set_var("expView", "none");
			$template->set_var("pwdView", "");
			$template->set_var("buttonView", "");
			$template->set_var("pageType", "e");
		}
	}
	
	if($type=="v")
	{	
		setTemplateWithSession($template);
		try{
			//construct new user instance
			$currentUser = new EKUser($id);
			$template->set_var("title", "Personal information");
			$template->set_var("params", "");
			$template->set_var("userId", $id);
			$template->set_var("name", $currentUser->getName());
			$template->set_var("email", $currentUser->getEmail());
			$template->set_var("selfIntro", $currentUser->getSelfIntro());
			
			$template->set_var("user_lv"," Lv: <strong>".$currentUser->getLevel()."</strong>");
			$template->set_var("exp",$currentUser->getExp());
			
			$template->set_var("ratingA", $currentUser->getRatingA());
			$template->set_var("ratingB", $currentUser->getRatingB());
			
		}catch(Exception $e)
		{
			$note = "<script language=javascript>\n".
			        "alert(\"".$e->getMessage()."\");\n".
			        "history.go(-1);\n".
			        "</script>";
			$template->set_var("note", $note);
		}
		
		$template->set_var("idView", "none");
		$template->set_var("idViewSpan", "");
		
		$template->set_var("formView", "none");
		$template->set_var("formViewSpan", "");
		
		$template->set_var("expView", "");
		$template->set_var("pwdView", "none");
		$template->set_var("buttonView", "none");
		$template->set_var("pageType", "v");
	}
	
	//parse & generate the output
	$template->parse("output","user");
	$template->p("output");
?>